package com.haiya.application.query.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haiya.application.query.CargoQueryService;
import com.haiya.application.query.dto.CargoDTO;
import com.haiya.application.query.qry.CargoFindbyCustomerQry;
import com.haiya.application.query.assembler.CargoDTOAssembler;
import com.haiya.infrastructure.db.dataobject.CargoDO;
import com.haiya.infrastructure.db.mapper.CargoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qiaoguoqiang
 */
@Service
public class CargoQueryServiceImpl implements CargoQueryService {
    @Autowired
    private CargoMapper cargoMapper;
    @Autowired
    private CargoDTOAssembler converter;

    @Override
    public List<CargoDTO> queryCargos() {
        QueryWrapper<CargoDO> queryWrapper = new QueryWrapper<>();
        List<CargoDO> dos = cargoMapper.selectList(queryWrapper);
        return dos.stream().map(converter::apply).collect(Collectors.toList());
    }

    @Override
    public List<CargoDTO> queryCargos(CargoFindbyCustomerQry qry) {
        QueryWrapper<CargoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sender_phone", qry.getCustomerPhone());
        List<CargoDO> dos = cargoMapper.selectList(queryWrapper);
        return dos.stream().map(converter::apply).collect(Collectors.toList());
    }

    @Override
    public CargoDTO getCargo(String cargoId) {
        return converter.apply(cargoMapper.selectById(cargoId));
    }
}
